package com.gome.boot.common.enums;

import java.io.Serializable;

/**
 * 各类正反状态枚举.
 * <br/>是/否
 * <br/>可以/不可以
 * <br/>启用/禁用
 *
 * @author baoxiufeng
 */
public enum StateEnum implements GenericEnum<Integer, String, String>, Serializable {
    NO(0, "0", "N"),
    YES(1, "1", "Y");

    private Integer id;
    private String code;
    private String name;

    StateEnum(Integer id, String code, String name) {
        this.id = id;
        this.code = code;
        this.name = name;
    }

    /**
     * 根据状态ID获取状态枚举.
     *
     * @param id 状态ID
     * @return 状态枚举
     */
    public static StateEnum getById(Integer id) {
        if (id != null) {
            StateEnum[] values = values();
            for (StateEnum value : values) {
                if (id.equals(value.id)) {
                    return value;
                }
            }
        }
        return NO;
    }

    /**
     * 根据状态代码获取状态枚举.
     *
     * @param code 状态代码
     * @return 状态枚举
     */
    public static StateEnum getByCode(String code) {
        if(code != null) {
            StateEnum[] values = values();
            for (StateEnum value : values) {
                if (code.equals(value.code)) {
                    return value;
                }
            }
        }
        return NO;
    }

    @Override
    public Integer getId() {
        return id;
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getName() {
        return name;
    }
}
